package com.example.shc;


import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.ToggleButton;
import de.hawhamburg.SmartHomeControll.ActionsVaras;
import de.hawhamburg.livingplace.messaging.android.AndroidPublisher;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity {




	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);


        SeekBar   slideBar_gardinen = (SeekBar) findViewById(R.id.seekBar_Gardinen);
        ToggleButton lightToggle = (ToggleButton) findViewById(R.id.lightToggleButton);
        ToggleButton loungeroom = (ToggleButton) findViewById(R.id.loungBtn);
		
		lightToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		        if (isChecked) {

		            sendMessage(diningLight_On());
		            // The toggle is enabled
                    showStringAsToast("Light is enabled");

                } else {
                    sendMessage(diningLight_Off());
		            // The toggle is disabled
		        	showStringAsToast("Light is disabled");

		        }
		    }
		});

        loungeroom.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    sendMessage(actionGardinen(ActionsVaras.openLounge));

                    // The toggle is enabled
                    showStringAsToast("Lounge open");


                }  else {
                    sendMessage(actionGardinen(ActionsVaras.closeLounge));

                    // The toggle is enabled
                    showStringAsToast("Lounge close");

                }


            }
        }
        );





	}


    private class sendMessageToProxy extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            try{
                AndroidPublisher androidPublisher = new AndroidPublisher(params[0], Integer.valueOf(params[1]),
                        params[2]);
                androidPublisher.setMessage(params[4]);

                if(params[3].equals("Topic"))   androidPublisher.publishToTopic();
                else androidPublisher.publishToQueue();

            } catch (IOException e){
                Log.e("Publisher", "Can´t publish the message");
            }
            return null;
        }
    }


    private void sendMessage(JSONObject jsonObject){
        String msg = jsonObject.toString();
        String[] params = new String[5];
        params[0] =  " 172.16.0.200";                       // IP des Proxy Servers
        params[1] =  Integer.toString(12349);       //    Port des Proxies
        params[2] = "LP.LIGHTCONTROL";                      // Topic für die Licht, Gardinen, Rolosteuerung
        params[3] = "topic";                                // oder "Queue"
        params[4] = msg.toString();
        sendMessageToProxy smtp = new sendMessageToProxy();
        smtp.execute(params);
    }

    private JSONObject diningLight_On() {
        Map<String, String> valueMap = new HashMap<String, String>();
        valueMap.put("red", "0");
        valueMap.put("green", "0");
        valueMap.put("blue", "0");
        valueMap.put("fadeTime", "0");

        Map<String, Object> aenderMap = new HashMap<String, Object>();
        aenderMap.put("values", new JSONObject(valueMap));
        aenderMap.put("Id", "client_1578909153");
        aenderMap.put("Version", null);
        aenderMap.put("action", "dinig_light_color");
        JSONObject msgFarbeEsszimmer = new JSONObject(aenderMap);
        Log.d("JSONObj", msgFarbeEsszimmer.toString());
        return msgFarbeEsszimmer;
    }




    private JSONObject diningLight_Off() {
        Map<String, String> valueMap = new HashMap<String, String>();
        valueMap.put("red", "255");
        valueMap.put("green", "255");
        valueMap.put("blue", "255");
        valueMap.put("fadeTime", "0");

        Map<String, Object> aenderMap = new HashMap<String, Object>();
        aenderMap.put("values", new JSONObject(valueMap));
        aenderMap.put("Id", "client_1578909153");
        aenderMap.put("Version", null);
        aenderMap.put("action", "dinig_light_color");
        JSONObject msgFarbeEsszimmer = new JSONObject(aenderMap);
        Log.d("JSONObj", msgFarbeEsszimmer.toString());
        return msgFarbeEsszimmer;
    }


    ////////////////////////////
    //  Fix Actions
    ///////



    private JSONObject actionGardinen(String action)  {
        Map<String, Object> chanceMap = new HashMap<String, Object>();
        chanceMap.put("values", "");
        chanceMap.put("Id", "client_1578909153");
        chanceMap.put("version", null);
        chanceMap.put("action", action);
        JSONObject msgGardinen = new JSONObject(chanceMap);
        Log.d("JSONObj", msgGardinen.toString());
        return msgGardinen;
    }


    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private void showStringAsToast (String text){
		Context context = getApplicationContext();
		int duration = Toast.LENGTH_SHORT;
		
		Toast.makeText(context, text, duration).show();
	}

}
